# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout


#registers a new user in the database
def registration(request):
    if request.method=='POST':

        #check that the password matched the re-type field
        if request.POST['password']==request.POST['repassword']:

            #checks the lengths of username and pass to ensure they are long enough
            if len(request.POST['username'])<3:
                return render_to_response('backend/registration.html',{'error_message':"User Name too short"})
            if len(request.POST['password'])<8:
                return render_to_response('backend/registration.html',{'error_message':"Password is to short required 8 characters"})

            #makes sure the user is not a repeat
            try:
                temp=User.objects.get(username=request.POST['username'])
                return render_to_response('backend/registration.html',{'error_message':"Username already exists"})
            except User.DoesNotExist:
                p=User.objects.create_user(request.POST['username'],request.POST['email'],request.POST['password'])
                p.is_staff=False
                p.save()
            #authenticates and logs in valid users
            user=authenticate(username=request.POST['username'],password=request.POST['password'])
            if user is not None:
                if user.is_active:
                    login(request,user)
                    return HttpResponseRedirect('../home') #logged in

                #the user account is disabled
                else:
                    print "account disabled"
                    return render_to_response('backend/registration.html',
                                              {'error_message':"Your account has been diabled"})

            #wrong username or password    
            else:
                print "incorrect username and password"
                return render_to_response('backend/registration.html',{'error_message':"Incorrect username or password"}) # wrong password

        #passwords did not match
        else:
            return render_to_response('backend/registration.html',{'error_message':"Your passwords did not match"}) ## missmatch password

    #wrong request method
    else:
        return render_to_response('backend/registration.html') # not posted

#logs out a user
def log_out(request):
    logout(request)
    return HttpResponseRedirect('../home')

#logs in a user
def log_in(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)

    #makes sure authenticate worked
    if user is not None:
        #if the user is active and authenticated, log in
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('../home')
        else:
            5/0 # fix
            print 'fail'
    else:
        return HttpResponseRedirect('../registration')


